// ParameterPackage.cpp: implementation of the CParameterPackage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "filecheck.h"
#include "ParameterPackage.h"
#include <fstream>
using namespace std;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


//////////////////////////////////////////////////////////////////////
#define CNWORD_FILE_PATHNAME		"D:\\ERROR_CN\\CN_words.txt"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CParameterPackage* CParameterPackage::_instance = 0;

CParameterPackage::CParameterPackage()
 : m_bStopRun(FALSE)
 , m_hSrchWnd(NULL)
 , m_hCheckWnd(NULL)
 , m_hViewLstWnd(NULL)
 , m_hMsgWnd(NULL)
 , m_bCreateXLSEngine(FALSE)
 , m_bCreateDOCEngine(FALSE)
{

}

CParameterPackage::~CParameterPackage()
{

}


CParameterPackage* CParameterPackage::Instance()
{
    if (_instance == 0)
	{
        _instance = new CParameterPackage;

		// initialize
		_instance->LoadCnWords();		
    }
    return _instance;
}

void CParameterPackage::Destroy()
{
	if ( _instance != 0 )
	{
		_instance->ReleaseList();	
		
		delete _instance;
		_instance = 0;
	}

}

void CParameterPackage::InitializeCS()
{
	InitializeCriticalSection(&m_cs);
}

void CParameterPackage::DeleteCS()
{
	DeleteCriticalSection(&m_cs);
}

void CParameterPackage::LoadCnWords()
{
	//
	// clear words cache
	//
	m_cnwordsLst.RemoveAll();


	//
	// load all string into buffer
	//

	fstream cnFile(CNWORD_FILE_PATHNAME, ios::in);
	if ( !cnFile.is_open() )
	{
		return;
	}


	char szBuf[1024];
	int nLen;
	TCHAR szNewBuf[1024];
	CString strWords;

	while ( !cnFile.eof() )
	{
		ZeroMemory(szBuf, 1024);
		ZeroMemory(szNewBuf, 1024);
		
		cnFile.getline(szBuf, 1024);
		CharUpperA(szBuf); // To upper case
		
		nLen = strlen(szBuf);
		if ( nLen > 0)
		{
			
#ifdef _UNICODE
			int cchWideChar = MultiByteToWideChar(CP_ACP, 0, szBuf, -1, szNewBuf, nLen);
#else	//	UNICODE
			strcpy(szNewBuf, szBuf);
			szNewBuf
#endif	//	_UNICODE
				
				strWords = szNewBuf;
			m_cnwordsLst.Add(strWords);
		}
	}

	cnFile.close();

	return;
}


void CParameterPackage::ReleaseList()
{
	// release all progress window handles in list control
	// m_myLstCtrl.ReleaseItems();


	// release memory resource	
	FILEINFO_ITERATOR pos = m_vecFileLst.begin();
	while ( pos != m_vecFileLst.end() )
	{
		delete (*pos);
		++pos;
	}

	m_vecFileLst.clear();

	LVITEM_ITERATOR pos01 = m_lvItemCache.begin();
	while ( pos01 != m_lvItemCache.end() )
	{
		delete (*pos01)->pszText;
		delete (*pos01);

		++pos01;
	}

	m_lvItemCache.clear();
}


LV_ITEM* CParameterPackage::CreateLvItem()
{
	LV_ITEM* plvItem = new LV_ITEM;
	plvItem->pszText = NULL;
	
	ZeroMemory(plvItem, sizeof(LV_ITEM));
	m_lvItemCache.push_back(plvItem);

	return plvItem;
}


